package com.wangshouyu.mp.menu;

import com.wangshouyu.mp.base.request.HttpMethod;
import com.wangshouyu.mp.base.request.MpClient;
import com.wangshouyu.mp.base.request.RequestBody;
import com.wangshouyu.mp.base.request.ResponseBody;
import com.wangshouyu.mp.base.response.BaseResponse;
import com.wangshouyu.mp.utils.GsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.MessageFormat;

import static com.wangshouyu.mp.base.constant.UrlConstant.*;

/**
 * @author 王守钰
 * @date 2020年12月06日22:44:26
 * @description: 微信菜单处理器
 */
public class MenuHandler {

    private MenuHandler() {
    }

    private static final Logger log = LoggerFactory.getLogger(MenuHandler.class);

    /**
     * 创建菜单
     * @param client 客户端
     * @param menu 菜单
     * @param accessToken 凭证
     * @return 返回结果
     */
    public static BaseResponse create(MpClient client, Menu menu, String accessToken){
        String url = MessageFormat.format(CREATE_MENU_URL, accessToken);
        String menuJson = GsonUtils.toJson(menu);
        log.info("{}", menuJson);
        return client.request(HttpMethod.POST,url, RequestBody.create(menuJson, ResponseBody.UTF_8), BaseResponse.class);
    }

    /**
     * 创建自定义菜单
     * @param client 客户端
     * @param menu 菜单
     * @param accessToken token
     * @return 返回结果
     */
    public static CustomMenuResponse createCustomer(MpClient client, CustomMenu menu, String accessToken){
        String url = MessageFormat.format(CREATE_PERSONALIZED_MENU_URL, accessToken);
        String menuJson = GsonUtils.toJson(menu);
        log.info("{}", menuJson);
        return client.request(HttpMethod.POST,url, RequestBody.create(menuJson, ResponseBody.UTF_8), CustomMenuResponse.class);
    }

    /**
     * 删除自定义菜单
     * @param client 客户端
     * @param menu 菜单
     * @param accessToken token
     * @return 返回结果
     */
    public static BaseResponse delCustomer(MpClient client, Menu menu, String accessToken){
        String url = MessageFormat.format(DEL_PERSONALIZED_MENU_UEL, accessToken);
        String menuJson = GsonUtils.toJson(menu);
        log.info("{}", menuJson);
        return client.request(HttpMethod.POST,url, RequestBody.create(menuJson, ResponseBody.UTF_8), BaseResponse.class);
    }

    /**
     * 获取菜单
     * @param client 客户端
     * @param accessToken token
     * @return 菜单列表
     */
    public static CustomMenuResponse menus(MpClient client, String accessToken){
        String url = MessageFormat.format(GET_MENU_URL, accessToken);
        return client.request(HttpMethod.GET,url, CustomMenuResponse.class);
    }

    /**
     * 删除自定义菜单
     * @param client 客户端
     * @param accessToken token
     * @return 返回结果
     */
    public static BaseResponse del(MpClient client, String accessToken){
        String url = MessageFormat.format(DEL_MENU_URL, accessToken);
        return client.request(HttpMethod.POST,url, BaseResponse.class);
    }

    /**
     * 匹配菜单
     * @param client 客户端
     * @param menuMatch 匹配信息
     * @param accessToken token
     * @return 匹配的菜单
     */
    public static CustomMenuResponse match(MpClient client, MenuMatch menuMatch, String accessToken){
        String url = MessageFormat.format(MATCH_MENU_URL, accessToken);
        String menuJson = GsonUtils.toJson(menuMatch);
        log.info("{}", menuJson);
        return client.request(HttpMethod.POST,url, RequestBody.create(menuJson, ResponseBody.UTF_8), CustomMenuResponse.class);

    }
}
